package com.activiti.advance;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;

import java.util.Date;

/**
 * 流程暂停、挂起
 *
 * @author ldanniel
 */
public class ProcessPause {
    public static void main(String[] args) {
        ProcessPause processPause = new ProcessPause();
        processPause.suspendSingleProcess();
    }

    public void suspendSingleProcess() {
        //创建
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        RuntimeService runtimeService = processEngine.getRuntimeService();
        //查询流程实例id:2501
        ProcessInstance processInstance = runtimeService.createProcessInstanceQuery()
                .processInstanceId("2501")
                .singleResult();
        //判断当前流程是否被挂起
        boolean suspended = processInstance.isSuspended();
        //获取流程实例id
        String processInstanceId = processInstance.getId();
        //如果是挂起，则改为激活状态
        if (suspended) {
            //激活：参数1：流程定义id，参数2：是否激活，参数3：激活时间
            runtimeService.activateProcessInstanceById(processInstanceId);
            System.out.println("流程实例id:" + processInstanceId + "，已激活");
        } else {
            //如果是激活状态，改为挂起状态 act_ru_task表 SUSPENSION_STATE_
            //参数1：流程定义id，参数2：是否暂停，参数3：暂停时间
            runtimeService.activateProcessInstanceById(processInstanceId);
            System.out.println("流程实例id:" + processInstanceId + "，已挂起");
        }
    }

    /**
     * 全部流程挂起
     */
    public void suspendAllProcess() {
        //创建
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        RepositoryService repositoryService = processEngine.getRepositoryService();
        //查询流程定义
        ProcessDefinition definition = repositoryService.createProcessDefinitionQuery()
                .processDefinitionKey("myEvection")
                .singleResult();
        //判断当前流程是否被挂起
        boolean suspended = definition.isSuspended();
        //获取流程定义id
        String definitionId = definition.getId();
        //如果是挂起，则改为激活状态
        if (suspended) {
            //激活：参数1：流程定义id，参数2：是否激活，参数3：激活时间
            repositoryService.activateProcessDefinitionById(definitionId, true, null);
            System.out.println("流程定义id:" + definitionId + "已激活");
        } else {
            //如果是激活状态，改为挂起状态 act_ru_task表 SUSPENSION_STATE_
            //参数1：流程定义id，参数2：是否暂停，参数3：暂停时间
            repositoryService.suspendProcessDefinitionById(definitionId, true, null);
            System.out.println("流程定义id:" + definitionId + "已挂起");
        }
    }
}
